using System;
using System.Globalization;
using ValidationFramework;
using ValidationFramework.Configuration;

namespace ExampleLibraryCSharp.NewRule
{
    public class RequiredCharacterRuleConfigReader : IRuleConfigReader
    {
        /// <summary>
        /// Create a <see cref="Rule"/> from a <see cref="RuleData"/>.
        /// </summary>
        /// <param name="ruleData">The <see cref="RuleData"/> that represent the xml to 
        /// create the <see cref="Rule"/> for.</param>
        /// <param name="runtimeTypeHandle">The <see cref="System.RuntimeTypeHandle"/> 
        /// for the <see cref="Type"/> to create the <see cref="Rule"/> for.</param>
        /// <returns>
        /// A <see cref="Rule"/> that <paramref name="ruleData"/> represented
        /// </returns>
        public Rule ReadConfig(RuleData ruleData, RuntimeTypeHandle runtimeTypeHandle)
        {
            if (ruleData == null)
            {
                throw new ArgumentNullException("ruleData");
            }
            var requiredCharacter =
                Convert.ToChar(ruleData.XmlAttributes["requiredCharacter"], CultureInfo.InvariantCulture);
            return new RequiredCharacterRule(ruleData.ErrorMessage, 
                ruleData.UseErrorMessageProvider, requiredCharacter);
        }
    }
}
